﻿
Imports System.IO
Imports std
Imports usshare

'this is a usapi, mainly for test usapilib and urlshorten xmlapi.

Public Module usapi
    Public Sub main(ByVal args() As String)
        If args.Length() < 3 Then
            raiseError("argument count error, serverAddress input output, " _
                       + "if input or output is " + consoleIOArgument + ", use console. " _
                       + "each line of input contains a command and other argument. " _
                       + "such as submit {key} {url}. " _
                       + "each line of output contains a response. " _
                       + "such as {submit result} {shorturl}." _
                       , errorHandle.errorType.warning)
        Else
            Dim reader As TextReader = Nothing
            Dim writer As TextWriter = Nothing

            assert(initialInput(args(1), reader), "file no found " + args(1))
            assert(initialOutput(args(2), writer), "file no found " + args(2))
            usapilib.initial(args(0))

            Dim input As String = Nothing
            input = reader.ReadLine()
            While Not input Is Nothing
                'ignore empty line for my bad habit
                If Not isemptystring(input) Then
                    Dim cmds As vector(Of String) = Nothing
                    strsepar(input, defaultValue.spaceChars + character.comma, cmds, character.quoteMark)
                    If Not cmds Is Nothing AndAlso cmds.size() > 0 Then
                        Select Case cmds(0).ToLower()
                            Case "submit", actionVerbs(action.submit)
                                Dim shorturl As String = Nothing
                                Dim submitResult As submitResult = usshare.submitResult.unknown
                                If usapilib.submit(cmds(1), cmds(2), shorturl, submitResult) Then
                                    writer.WriteLine(usshare.display(submitResult) + character.tab + shorturl)
                                Else
                                    writer.WriteLine(usshare.display(submitResult))
                                End If
                            Case "preview", actionVerbs(action.preview)
                                Dim url As String = Nothing
                                If usapilib.preview(cmds(1), url) Then
                                    writer.WriteLine(url)
                                Else
                                    writer.WriteLine("{failed to preview}")
                                End If
                            Case "urlkeys", actionVerbs(action.urlkeys)
                                Dim keys As [set](Of String) = Nothing
                                If usapilib.urlkeys(cmds(1), keys) Then
                                    Dim it As [set](Of String).iterator = Nothing
                                    it = keys.begin()
                                    While it <> keys.end()
                                        writer.Write(+it)
                                        it += 1
                                        If it <> keys.end() Then
                                            writer.Write(character.tab)
                                        Else
                                            writer.WriteLine()
                                        End If
                                    End While
                                Else
                                    writer.WriteLine("{failed to urlkeys}")
                                End If
                            Case "have", actionVerbs(action.have)
                                If usapilib.have(cmds(1)) Then
                                    writer.WriteLine("{have}")
                                Else
                                    writer.WriteLine("{have not}")
                                End If
                            Case Else
                                writer.WriteLine("{not supported command " + input + "}")
                        End Select
                    Else
                        writer.WriteLine("failed to parse command " + input)
                    End If
                Else
                    'insert an empty line if input is also emptystring
                    writer.WriteLine()
                End If
                input = reader.ReadLine()
            End While

            closereader(reader)
            closewriter(writer)
        End If
    End Sub
End Module
